package ee.falcon.controller.profile;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import ee.falcon.gui.frame.NewOrEditProfileFrame;
import ee.falcon.gui.frame.ProfilesFrame;
import ee.falcon.xml.GreatestIdDetector;
import ee.falcon.xml.ProfileInfoList;
import ee.falcon.xml.ProfileInfoXMLModifier;
import ee.falcon.xml.SpecificProfileInfo;

public class SaveButtonListener implements ActionListener
{
	private NewOrEditProfileFrame newOrEditProfileFrame;
	private boolean is_edit_profile_frame; 
	private SpecificProfileInfo editableProfile; 
	private SpecificProfileInfo newProfile; 
	private ProfileInfoXMLModifier profileInfoXMLModifier; 
	private ProfilesFrame profilesFrame; 
	private ProfileInfoList profileInfoList; 

	public SaveButtonListener(NewOrEditProfileFrame newOrEditProfileFrame, 
			ProfileInfoXMLModifier profileInfoXMLModifier, 
			ProfileInfoList profileInfoList, ProfilesFrame profilesFrame)
	{
		this.newProfile = new SpecificProfileInfo(); 	
		this.newOrEditProfileFrame = newOrEditProfileFrame; 
		this.is_edit_profile_frame = newOrEditProfileFrame.getIsEditProfileFrame(); 
		this.editableProfile = newOrEditProfileFrame.getEditableProfile();  
		this.profileInfoXMLModifier = profileInfoXMLModifier; 
		this.profileInfoList = profileInfoList; 
		this.profilesFrame = profilesFrame; 
	}
	
	@Override
	public void actionPerformed(ActionEvent saveButtonEvent) 
	{
		newOrEditProfileFrame.validateInputs();
		if (newOrEditProfileFrame.getIsValidationSuccessful())
		{	
			if (get_is_edit_profile_frame())
			{
				fillSpecificProfileInfoFields(editableProfile);
				//new data that recently filled SpecificProfileInfo data structure now contains 
				//has to be updated in xml file aswell. 
				profileInfoXMLModifier.updateProfileInProfileInfoXML(editableProfile);  
			}
			else
			{
				fillSpecificProfileInfoFields(newProfile);	
				profileInfoXMLModifier.writeNewProfileToProfileInfoXML(newProfile,
						new GreatestIdDetector());
			}	
			//Now lets update JList and HasMap data aswell
			profileInfoList.renewData();
			//for better mocking, following method was made
			renewProfilesFrameJListData();
			profilesFrame.setEnabled(true); 
			newOrEditProfileFrame.setVisible(false); 
			newOrEditProfileFrame.dispose();	
		}	
	}
	
	public void fillSpecificProfileInfoFields(SpecificProfileInfo specificProfileToFill)
	{
		specificProfileToFill.setHost(newOrEditProfileFrame.getHostNamejTextField().getText()); 
		specificProfileToFill.setUser(newOrEditProfileFrame.getUserNameJTextField().getText());
		specificProfileToFill.setPassword(newOrEditProfileFrame.getJPasswordFieldValue()); 
		specificProfileToFill.setPort(Integer.parseInt(newOrEditProfileFrame.getPortNumberJSpinner()
				.getValue().toString())); 
		specificProfileToFill.setProtocol(newOrEditProfileFrame.getFileProtocolJComboBox().
				getSelectedItem().toString()); 
		specificProfileToFill.setProtocolEncryptionType(newOrEditProfileFrame.getEncryptionTypeJComboBox().
				getSelectedItem().toString());
	}

	//[START]GETTERS AND SETTERS
	public NewOrEditProfileFrame getNewOrEditProfileFrame()
	{
		return this.newOrEditProfileFrame; 
	}
	
	public void set_is_edit_profile_frame(boolean isEditProfileFrameOrNewProfileFrame)
	{
		this.is_edit_profile_frame = isEditProfileFrameOrNewProfileFrame; 
	}
	
	public boolean get_is_edit_profile_frame()
	{
		return this.is_edit_profile_frame; 
	} 
	
	public void setEditableProfile(SpecificProfileInfo editableProfile)
	{
		this.editableProfile = editableProfile; 
	}
	
	public SpecificProfileInfo getEditableProfile()
	{
		return this.editableProfile; 
	}
	
	public void setNewProfile(SpecificProfileInfo newProfile)
	{
		this.newProfile = newProfile; 
	}
	
	public SpecificProfileInfo getNewProfile()
	{
		return this.newProfile;
	}
	
	public ProfileInfoXMLModifier getProfileInfoXMLModifier()
	{
		return this.profileInfoXMLModifier; 
	}

	public ProfilesFrame getProfilesFrame()
	{
		return this.profilesFrame;
	}
	
	public ProfileInfoList getProfilesInfoList()
	{
		return this.profileInfoList;
	}
	
	public void renewProfilesFrameJListData()
	{
		profilesFrame.getProfilesFrameJListEditor().renewJListData(); 
	}	
	//[END]GETTERS AND SETTERS
}